/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package com.xiashitech.agent.instrumentation.okhttp.v3;

import com.xiashitech.agent.instrumentation.base.propagator.W3CReentryBaggagePropagator;
import com.xiashitech.agent.instrumentation.bridge.Java8Bridge;
import com.xiashitech.agent.instrumentation.okhttp.common.ResponseHeaderGetter;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

public class XSTracingInterceptor implements Interceptor {

  public static final XSTracingInterceptor tracingInterceptor = new XSTracingInterceptor();

  @Override
  public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    Response response = chain.proceed(request);
    W3CReentryBaggagePropagator.getInstance().extract(Java8Bridge.currentForXSContext(), response, ResponseHeaderGetter.INSTANCE).makeCurrent();
    return response;
  }
}
